//
//  ReadHeadView.m
//  Lucky28
//
//  Created by zr on 2018/7/31.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "ReadHeadView.h"

@interface ReadHeadView ()

@property (nonatomic, strong)UIButton * oneButton;
@property (nonatomic, strong)UIButton * twoButton;
@property (nonatomic, strong)UIButton * threeButton;
@property (nonatomic, strong)UIButton * fourButton;
@property (nonatomic, strong)UIButton * fiveButton;

@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UIView * threeLineView;
@property (nonatomic, strong)UIView * fourLineView;
@property (nonatomic, strong)UIView * fiveLineView;

@end

@implementation ReadHeadView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    return self;
}


- (void)setupUI{
    
    self.oneButton = [self setupButtonWith:@"精选"];
    self.oneButton.selected = YES;
    
    [self addSubview:self.oneButton];
    
    self.twoButton = [self setupButtonWith:@"人生规划"];
    
    [self addSubview:self.twoButton];
    
    self.threeButton = [self setupButtonWith:@"心灵鸡汤"];
    
    [self addSubview:self.threeButton];
    
    self.fourButton = [self setupButtonWith:@"感情挽回"];
    
    [self addSubview:self.fourButton];
    
    self.fiveButton = [self setupButtonWith:@"婚姻关系"];
    
    [self addSubview:self.fiveButton];
    
    
    
    self.oneLineView = [self setupLineView];
    self.oneLineView.hidden = NO;
    
    [self addSubview:self.oneLineView];
    
    self.twoLineView = [self setupLineView];
    
    [self addSubview:self.twoLineView];
    
    self.threeLineView = [self setupLineView];
    
    [self addSubview:self.threeLineView];
    
    self.fourLineView = [self setupLineView];
    
    [self addSubview:self.fourLineView];
    
    self.fiveLineView = [self setupLineView];
    
    [self addSubview:self.fiveLineView];
}


- (void)layoutSubviews{
    
    float width = SCREEN_WIDTH / 5 * SCREENH6 - 4;
    
    [self.oneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(15 * SCREENH6);
        make.leading.mas_equalTo(self);
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(30 * SCREENH6);
    }];
    
    [self.twoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.mas_equalTo(self.oneButton);
        make.leading.mas_equalTo(self.oneButton.mas_trailing);
    }];
   
    [self.threeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.mas_equalTo(self.twoButton);
        make.leading.mas_equalTo(self.twoButton.mas_trailing);
    }];
    
    [self.fourButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.mas_equalTo(self.threeButton);
        make.leading.mas_equalTo(self.threeButton.mas_trailing);
    }];
    
    [self.fiveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.mas_equalTo(self.fourButton);
        make.leading.mas_equalTo(self.fourButton.mas_trailing);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.oneButton.mas_bottom).offset(5 * SCREENH6);
        make.height.mas_equalTo(3 * SCREENH6);
        make.width.mas_equalTo(20 * SCREENH6);
        make.centerX.mas_equalTo(self.oneButton);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.mas_equalTo(self.oneLineView);
        make.centerX.mas_equalTo(self.twoButton);
    }];
    
    [self.threeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.mas_equalTo(self.twoLineView);
        make.centerX.mas_equalTo(self.threeButton);
    }];
    
    [self.fourLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.mas_equalTo(self.threeLineView);
        make.centerX.mas_equalTo(self.fourButton);
    }];
    
    [self.fiveLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.mas_equalTo(self.fourLineView);
        make.centerX.mas_equalTo(self.fiveButton);
    }];
}


- (UIButton *)setupButtonWith:(NSString *)string{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:string forState:UIControlStateNormal];
    [button setTitleColor:ColorWithRGB(0x585858) forState:UIControlStateNormal];
    [button setTitleColor:ColorWithRGB(0xF52F4C) forState:UIControlStateSelected];
    button.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}


- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xF52F4C);
    lineView.hidden = YES;
    
    return lineView;
}


- (void)clickButton:(UIButton *)button{
    
    NSString * type;
    
    if (button == self.oneButton) {
        
        if (!self.oneButton.selected) {
            
            self.oneLineView.hidden = NO;
            self.oneButton.selected = YES;
            
            self.twoButton.selected = NO;
            self.twoLineView.hidden = YES;
            self.threeButton.selected = NO;
            self.threeLineView.hidden = YES;
            self.fourButton.selected = NO;
            self.fourLineView.hidden = YES;
            self.fiveButton.selected = NO;
            self.fiveLineView.hidden = YES;
            
            type = @"10";
        }
    }else if (button == self.twoButton){
        
        if (!self.twoButton.selected) {
            
            self.twoLineView.hidden = NO;
            self.twoButton.selected = YES;
            
            self.oneButton.selected = NO;
            self.oneLineView.hidden = YES;
            self.threeButton.selected = NO;
            self.threeLineView.hidden = YES;
            self.fourButton.selected = NO;
            self.fourLineView.hidden = YES;
            self.fiveButton.selected = NO;
            self.fiveLineView.hidden = YES;
            
            type = @"11";
        }
    }else if (button == self.threeButton){
        
        if (!self.threeButton.selected) {
            
            self.threeLineView.hidden = NO;
            self.threeButton.selected = YES;
            
            self.oneButton.selected = NO;
            self.oneLineView.hidden = YES;
            self.twoButton.selected = NO;
            self.twoLineView.hidden = YES;
            self.fourButton.selected = NO;
            self.fourLineView.hidden = YES;
            self.fiveButton.selected = NO;
            self.fiveLineView.hidden = YES;
            
            type = @"12";
        }
    }else if (button == self.fourButton){
        
        if (!self.fourButton.selected) {
            
            self.fourLineView.hidden = NO;
            self.fourButton.selected = YES;
            
            self.oneButton.selected = NO;
            self.oneLineView.hidden = YES;
            self.threeButton.selected = NO;
            self.threeLineView.hidden = YES;
            self.twoButton.selected = NO;
            self.twoLineView.hidden = YES;
            self.fiveButton.selected = NO;
            self.fiveLineView.hidden = YES;
            
            type = @"13";
        }
    }else if (button == self.fiveButton){
        
        if (!self.fiveButton.selected) {
            
            self.fiveLineView.hidden = NO;
            self.fiveButton.selected = YES;
            
            self.oneButton.selected = NO;
            self.oneLineView.hidden = YES;
            self.threeButton.selected = NO;
            self.threeLineView.hidden = YES;
            self.fourButton.selected = NO;
            self.fourLineView.hidden = YES;
            self.twoButton.selected = NO;
            self.twoLineView.hidden = YES;
            
            type = @"14";
        }
    }
    
    NSDictionary * dict = @{@"type":type};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"getReadData" object:nil userInfo:dict];
}


@end
